﻿using System.Web.UI;
using System.Web.UI.WebControls;

namespace UDS.Web.UI.Controls
{
    /// <summary>
    /// Display the top title of a page.
    /// </summary>
    [ParseChildren(ChildrenAsProperties = true, DefaultProperty = "RightPanel")]
    public class PageTitle : WebControl
    {
        /// <summary>
        /// Gets or sets the controls which will be displayed at the right side in the control.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public Control RightPanel
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the controls which will be displayed as the title of the control.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Control TitlePanel
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the title of a page.
        /// </summary>
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Title
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the image path.
        /// </summary>
        [PersistenceMode(PersistenceMode.Attribute)]
        public string HintImage
        {
            get;
            set;
        }

        protected override void OnInit(System.EventArgs e)
        {
            EnsureChildControls();
            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            if (RightPanel != null)
            {
                Controls.Add(RightPanel);
            }
            if (TitlePanel != null)
            {
                Controls.Add(TitlePanel);
            }
            base.CreateChildControls();
        }

        /// <summary>
        /// Renders the content of the control the client.
        /// </summary>
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<table class=\"treeview_header_table\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
            writer.Write("<tr>");
            writer.Write("<td class=\"treeview_header\">");

            // Hint image
            if (!string.IsNullOrEmpty(HintImage))
            {
                writer.WriteBeginTag("img");
                writer.WriteAttribute("src", ResolveClientUrl(HintImage));
                writer.WriteAttribute("border", "0");
                writer.WriteAttribute("align", "absMiddle");
                writer.Write(" /> ");
            }

            // Title
            writer.Write("<b>");
            if (TitlePanel != null)
            {
                TitlePanel.RenderControl(writer);
            }
            else
            {
                writer.WriteEncodedText(Title);
            }
            writer.Write("</b>");

            writer.Write("</td>");

            // Buttons
            if (RightPanel != null)
            {
                writer.Write("<td class=\"treeview_header\" align=\"right\">");
                RightPanel.RenderControl(writer);
                writer.Write("</td>");
            }

            writer.Write("</tr>");
            writer.Write("</table>");
        }
    }
}